home *** CD-ROM | disk | FTP | other *** search
- unit Logging;
-
- interface
-
- { A simple logging device to write info to a text file then optionally display }
- { the log (for non-console apps), using notepad, when the program terminates. }
-
- procedure Log(const Msg: string);
- procedure LogFmt(const Msg: string; const Args: array of const);
-
- implementation
-
- uses Windows, SysUtils, Classes, Controls, Forms;
-
- type
- TLogger = class
- private
- FStream: TFileStream;
- public
- constructor Create;
- destructor Destroy; override;
- class function LogFileName: string;
- procedure WriteToStream(const Text: string);
- procedure WriteToStreamLn(const Text: string);
- end;
-
- const
- LogStream: TLogger = nil;
-
- constructor TLogger.Create;
- begin
- FStream := TFileStream.Create(LogFileName,fmCreate);
- end;
-
- destructor TLogger.Destroy;
- begin
- FStream.Free;
- inherited Destroy;
- end;
-
- class function TLogger.LogFileName: string;
- begin
- Result := Concat(ExtractFilePath(Application.ExeName),'LOG.TXT');
- end;
-
- procedure TLogger.WriteToStream(const Text: string);
- begin
- FStream.Write(Text[1],Length(Text));
- end;
-
- procedure TLogger.WriteToStreamLn(const Text: string);
- begin
- WriteToStream(Text+#13#10);
- end;
-
- procedure Log(const Msg: string);
- begin
- if not Assigned(LogStream) then
- LogStream := TLogger.Create;
- LogStream.WriteToStreamLn(Msg);
- end;
-
- procedure LogFmt(const Msg: string; const Args: array of const);
- begin
- Log(Format(Msg,Args));
- end;
-
- var
- ExitSave: Pointer;
-
- {==============================================================================}
- procedure UnitExitProc; Far;
- var
- wrkarea: array[0..255] of char;
-
- begin
- ExitProc:=ExitSave;
- if Assigned(LogStream) then
- begin
- LogStream.Free;
- if not IsConsole then
- if MessageBox(0,'Show log file?','Log results',MB_YESNO) = mrYes then
- begin
- StrPCopy(Wrkarea,'notepad ' + LogStream.LogFileName);
- WinExec(wrkarea,SW_SHOWMAXIMIZED);
- end;
- end;
- end;
- {==============================================================================}
- Begin
- ExitSave:=ExitProc;
- ExitProc:=@UnitExitProc;
- end.
-